package com.zl.autocode.service.autocode.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zl.autocode.common.constant.AutocodeEnum;
import com.zl.autocode.dao.autocode.ZlValidationMapper;
import com.zl.autocode.dto.autocode.validation.ReqValidationAddDTO;
import com.zl.autocode.dto.autocode.validation.ReqValidationDelDTO;
import com.zl.autocode.dto.autocode.validation.ReqValidationDetailDTO;
import com.zl.autocode.dto.autocode.validation.ReqValidationEditDTO;
import com.zl.autocode.dto.autocode.validation.ReqValidationQueryDTO;
import com.zl.autocode.dto.autocode.validation.ReqValidationStateDTO;
import com.zl.autocode.dto.autocode.validation.ResValidationAddDTO;
import com.zl.autocode.dto.autocode.validation.ResValidationDelDTO;
import com.zl.autocode.dto.autocode.validation.ResValidationDetailDTO;
import com.zl.autocode.dto.autocode.validation.ResValidationEditDTO;
import com.zl.autocode.dto.autocode.validation.ResValidationQueryDTO;
import com.zl.autocode.dto.autocode.validation.ResValidationStateDTO;
import com.zl.autocode.dto.autocode.validation.ResValidationVO2;
import com.zl.autocode.entity.autocode.ZlValidation;
import com.zl.autocode.entity.autocode.po.ZlValidationVo;
import com.zl.autocode.service.autocode.IValidationService;
import com.zl.autocode.service.base.AutocodeBaseManager;
import com.zl.common.dto.ReqDTO;
import com.zl.common.dto.pagination.PageReqVO;
import com.zl.common.enums.resp.GlobalRespEnum;
import com.zl.common.exception.BizException;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 校验规则
 * @author ZL
 */
@Service("autocodeValidationService")
public class ValidationServiceImpl extends AutocodeBaseManager implements IValidationService {

    @Autowired
    private ZlValidationMapper validationMapper;

    @Override
    public ResValidationQueryDTO queryValidationList(ReqDTO<ReqValidationQueryDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqValidationQueryDTO reqData = reqInfo.getReqData();
        PageReqVO page = pageHandle(reqInfo.getPageInfo());//处理分页数据
        ResValidationQueryDTO resDTO = BeanUtil.copyProperties(page, ResValidationQueryDTO.class);
        if(StringUtils.isNotBlank(reqData.getValidationTitle())){
            reqData.setValidationTitle("%"+reqData.getValidationTitle()+"%");
        }
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData,false,true);
        
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlValidationVo> list = validationMapper.selectListPage(paramMap);
        resDTO.setData(BeanUtil.copyToList(list, ResValidationVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResValidationDetailDTO queryValidationDetail(ReqDTO<ReqValidationDetailDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqValidationDetailDTO reqData = reqInfo.getReqData();
        ResValidationDetailDTO res = new ResValidationDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        ZlValidation view = validationMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResValidationAddDTO insertValidationInfo(ReqDTO<ReqValidationAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqValidationAddDTO reqData = reqInfo.getReqData();
        ResValidationAddDTO res = new ResValidationAddDTO();
        Date time = getSystemTime().getDate();
        
        ZlValidation record = new ZlValidation();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setValidationId(IdUtil.simpleUUID());
        
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("validationId",record.getValidationId());
        //检查主键是否冲突
        if(validationMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalRespEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        validationMapper.insertSelective(record);
        
        ZlValidation keyInfo = validationMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResValidationEditDTO updateValidationInfo(ReqDTO<ReqValidationEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqValidationEditDTO reqData = reqInfo.getReqData();
        ResValidationEditDTO res = new ResValidationEditDTO();
        Date time = getSystemTime().getDate();
        
        ZlValidation record = new ZlValidation();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        validationMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("validationId",record.getValidationId());
        ZlValidation keyInfo = validationMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResValidationDelDTO deleteValidationInfo(ReqDTO<ReqValidationDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqValidationDelDTO reqData = reqInfo.getReqData();
        ResValidationDelDTO res = new ResValidationDelDTO();
        Date time = getSystemTime().getDate();
        
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData);
        validationMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResValidationStateDTO updateValidationState(ReqDTO<ReqValidationStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqValidationStateDTO reqData = reqInfo.getReqData();
        ResValidationStateDTO res = new ResValidationStateDTO();
        Date time = getSystemTime().getDate();
         
        ZlValidation record = new ZlValidation();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        validationMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("validationId",record.getValidationId());
        ZlValidation keyInfo = validationMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}